package com.qingcloud.adminbackend.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.qingcloud.adminbackend.entity.config.SysDictionaryConfig;
import com.qingcloud.adminbackend.entity.sensor.SysSensorFactorConfigDTO;

import java.util.Map;

/**
 * 数据工具类
 *
 * @author yangfan
 * @since 2021/11/8 9:49
 */
public class DataUtil {


    /**
     * 得到字典翻译
     * @param dictMap 字典map
     * @param codeStr code字符串 多个以“,”分隔
     * @return 字典翻译
     */
    public static String getDictByStr(Map<String, SysDictionaryConfig> dictMap, String codeStr) {
        if(StrUtil.isBlank(codeStr)){
            return codeStr;
        }
        StringBuilder stringBuilder = new StringBuilder();
        String[] codeArr = codeStr.split(",");
        if(CollectionUtil.isNotEmpty(dictMap)){
            for(String code : codeArr){
                SysDictionaryConfig sysDictionaryConfig = dictMap.get(code);
                if(sysDictionaryConfig!=null){
                    stringBuilder.append(sysDictionaryConfig.getName())
                            .append(",");
                }else{
                    stringBuilder.append(code)
                            .append(",");
                }
            }
        }
        String resultStr = stringBuilder.toString();
        if(resultStr.endsWith(",")){
            resultStr = resultStr.substring(0,resultStr.length()-1);
        }
        return resultStr;
    }

    /**
     *
     * @param keyMap 监测因子keymap数据
     * @param sensorfactorArr 监测因子字符串数据 用逗号分隔处理
     * @return 监测因子字符串
     */
    public static String getSensorFactorStr(Map<Integer, SysSensorFactorConfigDTO> keyMap, String sensorfactorArr) {
        if(StrUtil.isBlank(sensorfactorArr)){
            return sensorfactorArr;
        }
        StringBuilder stringBuilder = new StringBuilder();
        String[] codeArr = sensorfactorArr.split(",");
        if(CollectionUtil.isNotEmpty(keyMap)){
            for(String code : codeArr){
                SysSensorFactorConfigDTO sensorFactorConfigDTO = keyMap.get(Integer.parseInt(code));
                if(sensorFactorConfigDTO!=null){
                    stringBuilder.append(sensorFactorConfigDTO.getFactorContent())
                            .append(",");
                }else{
                    stringBuilder.append(code)
                            .append(",");
                }
            }
        }
        String resultStr = stringBuilder.toString();
        if(resultStr.endsWith(",")){
            resultStr = resultStr.substring(0,resultStr.length()-1);
        }
        return resultStr;
    }
}
